Functions are blocks of code identified by a name, which can receive ""predetermined"" parameters or not ;).
In Python, functions:
Syntax:
def func(parameter1, parameter2=default_value):
"""
Doc String
"""
<code block>
return value
NOTE: The parameters with default value must be declared after the ones without default value.
In [3]:
def caps(val):
"""
caps returns double the value of the provided value
"""
return val*2
a = caps("TEST ")
print(a)
print(caps.__doc__)
In the above example, we have caps
as function, which takes val
as argument and returns val * 2
.
In [6]:
a = caps(1234)
print(a)
Functions can return any data type, next example returns a boolean value.
In [5]:
def isValid(data):
if 10 in data:
return True
return False
a = isValid([10, 200, 33, "asf"])
print(a)
In [22]:
a = isValid((10,))
print(a)
In [25]:
isValid((10,))
Out[25]:
In [7]:
a = isValid((110,))
print(a)
In [8]:
def isValid_new(data):
return 10 in data
print(isValid_new([10, 200, 33, "asf"]))
In [9]:
a = isValid_new((110,))
print(a)
Example (factorial without recursion):
In [11]:
def fatorial(n):#{
n = n if n > 1 else 1
j = 1
for i in range(1, n + 1):
j = j * i
return j
#}
# Testing...
for i in range(1, 6):
print (i, '->', fatorial(i))
Example (factorial with recursion):
In [12]:
def factorial(num):
"""Fatorial implemented with recursion."""
if num <= 1:
return 1
else:
return(num * factorial(num - 1))
# Testing factorial()
print (factorial(5))
# 5 * (4 * (3 * (2) * (1))
Example (Fibonacci series with recursion):
In [13]:
def fib(n):
"""Fibonacci:
fib(n) = fib(n - 1) + fib(n - 2) se n > 1
fib(n) = 1 se n <= 1
"""
if n > 1:
return fib(n - 1) + fib(n - 2)
else:
return 1
# Show Fibonacci from 1 to 5
for i in [1, 2, 3, 4, 5]:
print (i, '=>', fib(i))
Example (Fibonacci series without recursion):
In [14]:
def fib(n):
# the first two values
l = [1, 1]
# Calculating the others
for i in range(2, n + 1):
l.append(l[i -1] + l[i - 2])
return l[n]
# Show Fibonacci from 1 to 5
for i in [1, 2, 3, 4, 5]:
print (i, '=>', fib(i))
In [15]:
def test(a, b):
print(a, b)
return a + b
print(test(1, 2))
test(b=1, a=2)
Out[15]:
In [18]:
def test_abc(a, b, c):
print(a, b, c)
return a + b + c
In [3]:
try:
test_abc(b=1, a=2, 3)
except SyntaxError as e:
print("error", e)
NOTE: We cannot have non-keyword arguments after keyword arguments
In [21]:
test_abc(2, c=3, b=2)
Out[21]:
In [23]:
test_abc(2, b=2, c=3)
Out[23]:
Functions can also not return anything like in the below example
In [24]:
def test_new(a, b, c):
pass
Functions can also return multiple values, usually in form of tuple.
In [25]:
def test(a, b):
print(a, b)
return a*a, b*b
x, a = test(2, 5)
print(x)
print(type(x))
print(a)
print(type(a))
In [27]:
print(type(test(2, 5)))
In [32]:
def test(a, b):
print(a, b)
return a*a, b*b, a*b
In [33]:
x = test(2 , 5)
print(x)
print(type(x))
In [34]:
In [47]:
def test(a, b):
print(a, b)
return a*a, b*b, "asdf"
x = test(2 , 5)
print(x)
print(type(x))
In [ ]:
In [48]:
def test(a=100, b=1000):
print(a, b)
return a, b
x = test(2, 5)
print(x)
print(test(10))
In [ ]:
In [49]:
def test(a=100, b=1000):
print(a, b)
return a, b
print(test(b=10))
print(test(101))
In [2]:
def test(d, c, a=100, b=1000):
print(d, c, a, b)
return d, c, a, b
x = test(c=2, d=10, b=5)
print(x)
x = test(1, 2, 3, 4)
print(x)
print(test(10, 2))
Example (RGB conversion):
In [5]:
def rgb_html(r=0, g=0, b=0):
"""Converts R, G, B to #RRGGBB"""
return '#%02x%02x%02x' % (r, g, b)
def html_rgb(color='#000000'):
"""Converts #RRGGBB em R, G, B"""
if color.startswith('#'): color = color[1:]
r = int(color[:2], 16)
g = int(color[2:4], 16)
b = int(color[4:], 16)
return r, g, b # a sequence
print (rgb_html(200, 200, 255))
print (rgb_html(b=200, g=200, r=255)) # what's happened?
print (html_rgb('#c8c8ff'))
Note: non-default argument's should always follow default argument
In [2]:
def test(d, a=100, c, b=1000):
print(d, c, a, b)
return d, c, a, b
x = test(c=2, d=10, b=5)
print(x)
x = test(1, 2, 3, 4)
print(x)
print(test(10, 2))
In [4]:
def test(c, d, a=100, b=1000):
print(d, c, a, b)
return d, c, a, b
x = test(c=2, d=10, b=5)
print(x)
x = test(1, 2, 3, 4)
print(x)
print(test(10, 2))
Observations:
- The arguments with default value must come last, after the non-default arguments.
- The default value for a parameter is calculated when the function is defined.
- The arguments passed without an identifier are received by the function in the form of a list.
- The arguments passed to the function with an identifier are received in the form of a dictionary.
- The parameters passed to the function with an identifier should come at the end of the parameter list.
Example of how to get all parameters:
In [18]:
# *args - arguments without name (list)
# **kargs - arguments with name (ditcionary)
def func(*args, **kargs):
print (args)
print (kargs)
func('weigh', 10, unit='k')
In the example, kargs
will receive the named arguments and args
will receive the others.
The interpreter has some builtin functions defined, including sorted()
, which orders sequences, and cmp()
, which makes comparisons between two arguments and returns -1 if the first element is greater, 0 (zero) if they are equal, or 1 if the latter is higher. This function is used by the routine of ordering, a behavior that can be modified.
Example:
In [21]:
def func(*args, **kargs):
print (args)
print (kargs)
a = {
"name": "Mohan kumar Shah",
"age": 24 + 1
}
func('weigh', 10, unit='k', val=a)
In [24]:
def func(*args):
print(args)
func('weigh', 10, "test")
In [25]:
data = [(4, 3), (5, 1), (7, 2), (9, 0)]
# Comparing by the last element
def _cmp(x, y):
return cmp(x[-1], y[-1])
print ('List:', data)
Python also has a builtin function eval()
, which evaluates code (source or object) and returns the value.
Example:
In [1]:
print (eval('12. / 2 + 3.3'))
In [1]:
def listing(lst):
for l in lst:
print(l)
d = {"Mayank Johri":40, "Janki Mohan Johri":68}
listing(d)
In [17]:
d = {
"name": "Mohan",
"age": 24
}
a = {
"name": "Mohan kumar Shah",
"age": 24 + 1
}
def process_dict(d=a):
print(d)
process_dict(d)
process_dict()